﻿//-----------------------------------------------------------------------
// <copyright file="LoggedInUserData.xaml.cs" company="FIT VUT">
//     Copyright (c) FIT VUT. All rights are reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BookCaching.Data;
using System.Windows.Resources;

namespace BookCaching.CustomControls
{
    /// <summary>
    /// Custom control displaying data of a logged user.
    /// </summary>
    public partial class LoggedInUserData : UserControl
    {        
        public LoggedInUserData()
        {
            this.InitializeComponent();
        }

        private User loggedUser;

        public LoggedInUserData(User loggedUser) : this () 
        {
            this.loggedUser = loggedUser;
            reloadValues();
        }

        public void reloadValues(){
            List<Book> booksSource = new List<Book>();
            foreach (Book book in loggedUser.BorrowedBooks)
            {
                booksSource.Add(book);
            }
            //try
            //{
            //    BitmapImage imageOnline = new BitmapImage(new Uri(loggedUser.ImageLink));
            //    imageOnline.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(ImageOnline_ImageFailed);
            //    this.userImage.Source = imageOnline;
            //}
            //catch (Exception e)
            //{
            //    int a = 10;
            //}

            this.usernameText.Text = String.Format("{0} {1}", loggedUser.FirstName, loggedUser.SurName);

            //this.currentlyReadingListBox.ItemsSource = booksSource;

            StreamResourceInfo sr = Application.GetResourceStream(new Uri("Images/unknown_user_1.jpg", UriKind.Relative));
            BitmapImage imageNoImage = new BitmapImage();
            imageNoImage.SetSource(sr.Stream);
            this.userImage.Source = imageNoImage;
        }

        private void ImageOnline_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
        }

        public event EventHandler ShowAllMyBooks;

        private void BookItemTemplate_Tap(object sender, GestureEventArgs e)
        {
        }

        private void AllMyBooksButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.ShowAllMyBooks != null)
            {
                this.ShowAllMyBooks.Invoke(this, new EventArgs());
            }
        }        
    }
}
